package org.jeecg.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.app.entity.ShopCatalog;
import org.jeecg.modules.app.entity.ShopInfo;
import org.jeecg.modules.app.entity.Support;
import org.jeecg.modules.app.mapper.ShopMapper;
import org.jeecg.modules.app.service.ShopService;
import org.jeecg.modules.app.vo.RemakeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author 李圳川
 * @version 1.0
 * @date 2022/6/13 22:43
 * @content
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    /**
     * 商家信息查询
     * @param createBy
     * @return
     */
    @Override
    public ShopInfo findShopInfoByCreateBy(String createBy) {
        QueryWrapper<ShopInfo> queryWrapper = new QueryWrapper<>();
        ShopInfo shopInfo = shopMapper.selectOne(
                queryWrapper.eq("create_by",createBy)
        );
        if(shopInfo!=null)
            shopInfo.setSupports(makeSupport());
        return shopInfo;
    }

    @Override
    public Page<ShopInfo> findShopByPage(Integer pageNum, Integer pageSize, String search)
    {
        QueryWrapper<ShopInfo> queryWrapper = new QueryWrapper<>();
        Page<ShopInfo> shopCatalogPage;
        if(StringUtils.isBlank(search)){
             shopCatalogPage = shopMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
        }else{
             shopCatalogPage = shopMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper.like("shop_catalog",search));
        }

        return shopCatalogPage;
    }

    /**
     * 更新商家对应评分
     * remakeVO 包含商家Id 顾客id 订单id  服务分 产品分
     */
    @Override
    public void upDateShopInfoScore(RemakeVo remakeVo) {

        /**
         *  1.先把商家信息查出来
         */
        QueryWrapper<ShopInfo> queryWrapper = new QueryWrapper<>();
        ShopInfo shopInfo = shopMapper.selectOne(queryWrapper.eq("id",remakeVo.getShopId()));

     //   System.out.println(shopInfo);

        /**
         * 2. 更新得分信息
         */

      //  商品总分
        Double shopMarkTotalscore = shopInfo.getShopMarkTotalscore() + remakeVo.getFoodScore();
       Integer shopMarkNumpeople = shopInfo.getShopMarkNumpeople() + 1;

        //服务总分
        Double serviceAttitudeTotalscore = shopInfo.getServiceAttitudeTotalscore() + remakeVo.getServiceScore();
        Integer serviceAttitudeNumpeople = shopInfo.getServiceAttitudeNumpeople() + 1;

        //平均分
        Double shopMark = shopMarkTotalscore/shopMarkNumpeople;
        Double serviceAttitude = serviceAttitudeTotalscore/serviceAttitudeNumpeople;

        Double serviceScore = (shopMark + serviceAttitude ) / 2.0;

        //存平商品均分
        shopInfo.setShopMarkTotalscore(shopMarkTotalscore);
        shopInfo.setShopMarkNumpeople(shopMarkNumpeople);

        //存平服务均分
        shopInfo.setServiceAttitudeTotalscore(serviceAttitudeTotalscore);
        shopInfo.setServiceAttitudeNumpeople(serviceAttitudeNumpeople);

        // 存总分
        shopInfo.setShopMark(shopMark);
        shopInfo.setServiceAttitude(serviceAttitude);
        shopInfo.setServiceScore(serviceScore);


        QueryWrapper<ShopInfo> queryWrapper1 = new QueryWrapper<>();
        shopMapper.update(shopInfo,queryWrapper1.eq("id",shopInfo.getId()));

    }

    public ArrayList<Support> makeSupport(){
        ArrayList<Support> supports = new ArrayList<>();

        Support support = new Support();
        support.setContent("新用户下单立减17元(不予其它活动叠加)");
        support.setName("首单");
        support.setType(0);
        supports.add(support);

        support = new Support();
        support.setContent("可用天天神劵");
        support.setName("神劵");
        support.setType(1);
        supports.add(support);

        support = new Support();
        support.setContent("满35减19，满25减10");
        support.setName("满减");
        support.setType(2);
        supports.add(support);

        return supports;

    }
}
